package com.pony.iphone.controller;

import com.alibaba.fastjson.JSON;
import com.pony.iphone.data.FileDB;
import com.pony.iphone.dto.QuestionAnswerDto;
import com.pony.iphone.dto.QuestionAnswerErrorDto;
import com.pony.iphone.service.QuestionService;
import com.pony.iphone.utils.AppHttpCodeEnum;
import com.pony.iphone.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pony
 * @version 1.0
 * @description: 题库增删改查功能
 * @create 2025/8/16 22:40
 */
@RestController
@RequestMapping("question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;
    /**
     * 上传一个问题以及问题的所有答案，Lua脚本将遇见的新题上交给服务器
     * @param qaDto 一个问题和及其答案
     * @return R 上传的情况结果返回
     */
    @PostMapping("/upload")
    public R upload(@RequestBody QuestionAnswerDto qaDto){
        return questionService.upload(qaDto);
    }

    /**
     * 获得一个问题的答案
     * @param qaDto 一个问题和所有答案选项
     * @return
     */
    @PostMapping("/getAnswer")
    public R getAnswer(@RequestBody QuestionAnswerDto qaDto){
        // 参数校验
        if (qaDto.getQuestion() == null || qaDto.getQuestion().isEmpty()){
            return R.error(AppHttpCodeEnum.ERROR)
                    .message("参数错误，问题为空");
        }
        if (qaDto.getLibraryName() == null || qaDto.getLibraryName().isEmpty()){
            return R.error(AppHttpCodeEnum.ERROR)
                    .message("参数错误，题库名称为空");
        }
        return questionService.getAnswer(qaDto);
    }

    /**
     * 获得一个问题的答案
     * @param questionAnswerErrorDtoList 一个问题和所有答案选项
     * @return R
     */
    @PostMapping("/uploadError")
    public R uploadError(@RequestBody ArrayList<QuestionAnswerErrorDto> questionAnswerErrorDtoList){
        return questionService.uploadError(questionAnswerErrorDtoList);
    }
}
